package com.ssyc.gsk_tk.adapter;

import android.content.Context;
import android.widget.TextView;

import com.ssyc.common.adapter.CommonAdapter;
import com.ssyc.common.adapter.ViewHolder;
import com.ssyc.gsk_tk.R;
import com.ssyc.gsk_tk.bean.ReadLvInfo;

import java.util.List;

/**
 * 阅读选择答案的适配器
 * Created by Administrator on 2018/9/4 0004.
 */

public class ReadChooseAnswerAdapter extends CommonAdapter<ReadLvInfo> {
    private Context context;
    private List<ReadLvInfo> datas;
    private int hasDown;

    public ReadChooseAnswerAdapter(int hasDown, Context context, List<ReadLvInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.context = context;
        this.datas = datas;
        this.hasDown = hasDown;
    }

    @Override
    public void convert(ViewHolder holder, ReadLvInfo readLvInfo) {
        //索引值A--F
        TextView tvIndex = holder.getView(R.id.tv_index);
        tvIndex.setText(readLvInfo.questionIndex);

        //答案内容
        TextView tvAnswer = holder.getView(R.id.tv_content);
        tvAnswer.setText(readLvInfo.questionContent);

        if (hasDown == 0) {
            //未完成
            //是否选中
            if (readLvInfo.isSelected) {
                //选中
                tvIndex.setTextColor(context.getResources().getColor(R.color.tk_tv_select));
                tvIndex.setBackgroundResource(R.drawable.tk_tv_select);
            } else {
                //未选中
                tvIndex.setTextColor(context.getResources().getColor(R.color.tk_tv_normal));
                tvIndex.setBackgroundResource(R.drawable.tk_tv_normal);
            }
        } else if (hasDown == 1) {
            //已完成
            if (readLvInfo.questionIndex.equals(readLvInfo.chooseAnswer)) {
                //如果当前项是用户选择的答案,进行用户颜色处理
                //如果用户选择的答案跟该题目的正确答案相同,则只显示用户颜色
                if (readLvInfo.questionIndex.equals(readLvInfo.rightAnswer)) {
                    //如果当前项是正确选项
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tk_tv_select));
                    tvIndex.setBackgroundResource(R.drawable.tk_tv_right_answer);
                }else{
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tk_tv_select));
                    tvIndex.setBackgroundResource(R.drawable.tk_tv_use_choose);
                }

            } else {
                //如果当前项不是用户选择的答案
                if (readLvInfo.questionIndex.equals(readLvInfo.rightAnswer)) {
                    //如果当前项是正确选项
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tk_tv_select));
                    tvIndex.setBackgroundResource(R.drawable.tk_tv_right_answer);
                } else {
                    //如果当前项不是正确答案
                    tvIndex.setTextColor(context.getResources().getColor(R.color.tk_tv_normal));
                    tvIndex.setBackgroundResource(R.drawable.tk_tv_normal);
                }
            }
        }
    }

    /**
     * 同步状态
     *
     * @param pos
     */
    public void upDateStete(int pos) {

        for (int i = 0; i < datas.size(); i++) {
            if (i == pos) {
                datas.get(i).isSelected = true;
            } else {
                datas.get(i).isSelected = false;
            }
        }
        notifyDataSetChanged();
    }
}
